﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Web.Caching;


namespace Helper
{
    public class ConfigHelper
    {   
        public static Config LoadConfig()
        {
            Config c = new Config()
            {
                SoVatToiDa = 200,
                SoAnhToiDa = 10,
                SoBaoTangToiDa = 5,
                thongtinlienhe = true,
                KichThuocAnhGioiThieu = "180px",
                KichThuocAnhSuuTap = "180px",
                KichThuocAnhGiaoLuu = "180px",
                KichThuocAnhSanDauGia = "180px",
                NguoiDatThoiGianGiaoLuu = "admin",
                SoNgayGiaoLuu = 100,
                NguoiDatThoiGianTimKiemQuanTam = "admin",
                SoNgayTimKiemQuanTam = 10,
                NguoiDatThoiGianDauGia = "admin",
                SoNgayDauGia = 5,
                PhiDauGia = "5%",
                ChongSpamDauGia = 5,
                SoNguoiQuanTam = 20,
                PhiGiaoHang = 0,
                NguoiTraPhi = 1,
                KieuChuyenTien = 1,
                GiaoDichChuyenTien = 0,
                MessageThongBaoKhiYeuCauGiaoLuu = "Yêu cầu giao lưu của bạn đã bị ban quản trị từ chối!",
                TomTatMucTinBaiSuKien = string.Empty,
            };
            string path = Path.Combine(HttpRuntime.AppDomainAppPath, "Setting.xml");
            if (File.Exists(path))
            {
                using (Stream s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    try
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(Config));
                        c = (Config)xmlSerializer.Deserialize(s);
                    }
                    catch
                    {  
                    }
                }
            }
            return c;
        }

        public static bool Save(Config data)
        {
            string path = Path.Combine(HttpRuntime.AppDomainAppPath, "Setting.xml");
            try
            {
                using (Stream s = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Config));
                    xmlSerializer.Serialize(s, data);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}